//
//  DHNoLoginViewController.m
//  Dresshere
//
//  Created by 张朋 on 2017/12/12.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHNoLoginViewController.h"
#import "DHLoginViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <ShareSDKExtension/ShareSDK+Extension.h>
#import "DHHomeViewController.h"
#import <JPush/JPUSHService.h>
#import "WXApi.h"
#import "ZPImageButton.h"
#import "DHTabBarViewController.h"

@interface DHNoLoginViewController ()

@property (weak, nonatomic) IBOutlet UIButton *lookButton;
@property (strong, nonatomic) ZPImageButton *wxButton;
@property (strong, nonatomic) ZPImageButton *loginButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayout;

@end

@implementation DHNoLoginViewController

 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(iPhoneX){
        self.topLayout.constant = 20;
    }
    
    // 去看看按钮
    self.lookButton.layer.borderWidth = 1;
    self.lookButton.layer.borderColor = [[UIColor colorWithHexString:@"#707070"] CGColor];
    self.lookButton.layer.cornerRadius = self.lookButton.height/2;
    [self.lookButton bk_addEventHandler:^(id sender) {
//        DHHomeViewController *homeController = [[DHHomeViewController alloc] init];
//        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:homeController];
//        kAppDelegate.window.rootViewController = navController;
        DHTabBarViewController *tabBarController = [DHTabBarViewController new];
        kAppDelegate.window.rootViewController = tabBarController;
    } forControlEvents:UIControlEventTouchUpInside];
    
    // 初始化登录按钮
    self.loginButton = [[ZPImageButton alloc] initWithFrame:CGRectMake(85, kScreenHeight - 83, kScreenWidth - 85 * 2, 43) title:@"账号密码登录" titleFont:[UIFont systemFontOfSize:15] titleColor:[UIColor whiteColor] image:[UIImage imageNamed:@"d"] buttonBackgroundColor:[UIColor colorWithHexString:@"#4C4D5C"]];
    self.loginButton.layer.cornerRadius = 43 / 2;
    WeakSelf(weakSelf)
    [self.loginButton bk_addEventHandler:^(id sender) {
        DHLoginViewController *loginController = [[DHLoginViewController alloc] init];
        [weakSelf.navigationController pushViewController:loginController animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginButton];
    
    self.wxButton = [[ZPImageButton alloc] initWithFrame:CGRectMake(85, self.loginButton.top - 15 - 43, kScreenWidth - 85 * 2, 43) title:@"通过微信登录" titleFont:[UIFont systemFontOfSize:15] titleColor:[UIColor whiteColor] image:[UIImage imageNamed:@"login_wechat"] buttonBackgroundColor:[UIColor colorWithHexString:@"#74D085"]];
    self.wxButton.layer.cornerRadius = 43 / 2;
    [self.view addSubview:self.wxButton];
    
    [self.wxButton bk_addEventHandler:^(id sender) {
        [NSObject showHud];
        [ShareSDK getUserInfo:SSDKPlatformTypeWechat
               onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error){
             if (state == SSDKResponseStateSuccess){
                 NSLog(@"uid=%@",user.uid);
                 NSLog(@"%@",user.credential);
                 NSLog(@"token=%@",user.credential.token);
                 NSLog(@"nickname=%@",user.nickname);
                 [[DHNetAPIClient shareDHNetAPIClient] request_wechat_loginWithParams:@{@"access_token":user.credential.token,@"open_id":user.credential.uid} andBlock:^(id data, NSError *error) {
                     [NSObject hideHUDQuery];
                     if (data) {
                         User *user = [User modelWithJSON:[data objectForKey:@"user"]];
                         user.status = [NSNumber numberWithInt:1];
                         user.token = [data objectForKey:@"token"];
                         [Login doLogin:user];
                         [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                             
                         }];
                         [[GlobalManager shareGlobalManager] refreshVIPInfoAndBlock:^(id data, NSError *error) {
                             
                         }];
                         [JPUSHService setTags:[NSSet setWithObject:@"IOS"] aliasInbackground:user._id];
                         if ([Login isLogin]) {
                             NSString *sexString = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SELECT_SEX];
                             if (sexString) {
                                 [[DHNetAPIClient shareDHNetAPIClient] request_user_sexWithParams:@{@"sex":sexString} andBlock:^(id data, NSError *error) {
                                     [kAppDelegate configHomeController];
                                 }];
                             }else{
                                 [kAppDelegate configHomeController];
                             }
                         }
                     }
                 }];
             }else{
                 NSLog(@"%@",error);
                 [NSObject hideHUDQuery];
                 [NSObject showHudTipStr:@"微信授权失败"];
             }
         }];
    } forControlEvents:UIControlEventTouchUpInside];
    
    //判断安装微信
    if ([ShareSDK isClientInstalled:SSDKPlatformTypeWechat]){
        self.wxButton.hidden = NO;
    } else {
        self.wxButton.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
